<template>
  <div class="app-container">
    <el-row>
      <el-button type="primary" plain icon="el-icon-plus" @click="addInsuranceType" style="margin-bottom: 20px">添加</el-button>
    </el-row>
    <el-table ref="table" :data="tableData">
      <el-table-column align="center" prop="password" label="密码">
      </el-table-column>
      <el-table-column align="center" prop="createTime" label="创建时间">
      </el-table-column>
      <el-table-column align="center" label="操作">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="editMsg(scope.row)">修改</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog :title="msgTitle" :visible.sync="dialogVisible" width="40%" v-loading="loading">
      <el-form :model="addForm" ref="addForm" :rules="rules" label-width="140px">
        <el-form-item :label="editId ? '新密码' : '密码'" style="width: 100%" prop="password">
          <el-input v-model="addForm.password" placeholder="密码"></el-input>
        </el-form-item>
        <el-form-item v-if="!!editId" label="旧密码" style="width: 100%" prop="oldPassword">
          <el-input v-model="addForm.oldPassword" placeholder="密码"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cance">取 消</el-button>
        <el-button type="primary" @click="determine">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
export default {
  data() {
    return {
      loading: false,
      msgTitle: "添加",
      editId: "",
      addForm: {
        password: "",
        oldPassword: "",
      },
      dialogVisible: false,
      tableData: [],
      tableHeight: 0,
      rules: {
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            validator: (rule, value, cb) => this.checkPassword(rule, value, cb),
            trigger: "blur",
          },
        ],
        oldPassword: [
          { required: true, message: "请输入旧密码", trigger: "blur" },
          {
            validator: (rule, value, cb) =>
              this.checkOldPassword(rule, value, cb),
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    checkPassword(rule, value, cb) {
      if (value.length < 6) {
        cb("密码最少6为");
      }
      cb();
    },
    checkOldPassword(rule, value, cb) {
      if (value.length < 6) {
        cb("密码最少6为");
      }
      if (value === this.addForm.password) {
        cb("新旧密码不能相同");
      }
      cb();
    },
    determine() {
      this.$refs.addForm.validate((valid) => {
        if (valid) {
          if (this.loading) {
            return;
          }
          this.loading = true;
          const url = this.editId ? "system/password" : "system/password";
          const func = this.editId ? "$put" : "$post";
          const params = this.editId
            ? { ...this.addForm, passwordId: this.editId }
            : this.addForm;
          if (!this.editId) {
            delete this.addForm.oldPassword;
          }
          this[func](url, params)
            .then((res) => {
              if (res && res.code * 1 === 200) {
                this.$message({
                  type: "success",
                  message: this.editId ? "修改成功" : "新增成功",
                });
                this.$refs.addForm.resetFields();
                this.dialogVisible = false;
                this.getTableData();
              }
            })
            .finally(() => {
              this.loading = false;
            });
        }
      });
    },
    cance() {
      this.$refs.addForm.resetFields();
      this.dialogVisible = false;
    },
    addInsuranceType() {
      this.$nextTick(() => {
        this.msgTitle = "添加";
        this.dialogVisible = true;
        this.addForm.password = "";
        this.editId = "";
      });
    },
    editMsg(row) {
      this.$nextTick(() => {
        this.msgTitle = "修改";
        this.dialogVisible = true;
        this.addForm.password = "";
        this.addForm.oldPassword = "";
        this.editId = row.passwordId;
      });
    },
    getTableData() {
      this.$get("/system/password/list").then((res) => {
        this.tableData = res.data;
      });
    },
  },
  mounted() {
    this.getTableData();
  },
};
</script>
<style scoped lang="scss">
.form-inline ::v-deep {
  width: 100%;

  .el-form-item__label {
    // padding: 0;
  }
}
</style>